package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class TaiwanNewsAnalyse implements AnalyseNews {

	//http://www.taiwan.cn/xwzx/PoliticsNews/201008/t20100819_1496387.htm
	private static final String[] regex={
		"http://.*[.]taiwan.cn/.*[0-9]+/t[0-9]+_[0-9]+[_0-9]*.htm[l]*",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		doc.select(".pageroll").remove();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		title=doc.select("div.zl_text h1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("div.st_center_05 h2").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h1").text();
			}
		}
		
		Elements select=doc.select("div.mainbox_02,div.fontsizebox");
		if(select!=null){
			select.select("div#news_recommend").remove();
			select.select("p[align=center]").remove();
			content=HtmlCleaner.getContentHtml(url,select);
		}
		if(StringUtils.isBlank(content)){			
			content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
		}
		
		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("span.zlt_info_l").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div.mainbox_01").text();
			if(StringUtils.isBlank(authorStr)){
				authorStr=doc.select("div.st_center_05 p").text();
				if(StringUtils.isBlank(authorStr)){
					authorStr=doc.select("div.author").text();
				}
			}
		}
		author=StringUtils.substringBetween(authorStr, "来源：", " ");
		if (StringUtils.isBlank(author)) {
			author=StringUtils.substringBetween(authorStr, "来源：", " ");
			if (StringUtils.isBlank(author)) {
				author=StringUtils.substringAfter(authorStr, "来源：");
			}
		}
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}
		date=DateUtils.matchDate(doc.select("div.mainbox_01").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.st_center_05 p").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("span.zlt_info_l").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.author").text());
				}
			}
		}
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://ent.taiwan.cn/list/201311/t20131125_5261199.htm";
		TaiwanNewsAnalyse test=new TaiwanNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
